/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.util.List;
import javabeans.Event;
import org.hibernate.HibernateException;
import org.hibernate.Query;

/**
 *
 * @author liweijiao
 */
public class IEventDaoImpl extends DAO implements IEventDao {

    public Event newEvent(Event event) {
        try {
            begin();
            getSession().save(event);
            commit();
            return event;
        } catch (HibernateException e) {
            rollback();
            return null;

        }

    }

    public Event updateEvent(Event event) {
        try {
            begin();
            getSession().update(event);
            commit();
            return event;
        } catch (HibernateException e) {
            rollback();
            return null;

        }

    }

    public boolean deleteEvent(Event event) {
        try {
            begin();
            getSession().delete(event);
            commit();
            return true;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            return false;

        }

    }

    public Event getEvent(int eid) {
        try {
            Query q = getSession().createQuery("from Event e where e.id = :id");
            q.setInteger("id", eid);

            Event event = (Event) q.uniqueResult();
            return event;
        } catch (HibernateException e) {
            return null;

        }
    }

    public List<Event> getEventList(int planid) {
        
        try {
            
            Query q = getSession().createQuery("from Event e where e.plan =:pid");
            q.setInteger("pid", planid);
             
            List resultlist =  q.list();
            List<Event> eventlist=(List<Event>) resultlist;
           
            return eventlist;
        } catch (HibernateException e) {
            return null;

        }
    }

    public List<Event> getGroupEventList(int groupplanid) {
         try {

            Query q = getSession().createQuery("from Event e where e.groupplan =:pid");
            q.setInteger("pid", groupplanid);

            List resultlist =  q.list();
            List<Event> eventlist=(List<Event>) resultlist;

            return eventlist;
        } catch (HibernateException e) {
            return null;

        }
    }
}
